package com.zlx.sharelive.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.zlx.module_base.base_api.res_data.VideoLikeListBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_fg.BaseFg;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.activity.ShortVideoDetailsAc;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.StringUt;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 评论 短视频
 */
public class CommentFabulousShortVideoFg extends BaseFg {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;
    @BindView(R.id.sw_layout)
    SwipeRefreshLayout sw_layout;

    private int page = 1;
    private List<VideoLikeListBean.InfoDTO.ResultDTO.RecordsDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<VideoLikeListBean.InfoDTO.ResultDTO.RecordsDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fg_fabulous_short_video;
    }

    @Override
    protected void initViews() {
        super.initViews();

        initRecyclerView();
        getVideoComment();

        sw_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getVideoComment();
            }
        });
    }


    /**
     * 获取短视频数据
     */
    private void getVideoComment() {
        ApiUtil.getHomeApi().getVideoComment(SPUtil.getToken(getContext()),
                "0", String.valueOf(page)).observe(getViewLifecycleOwner(), new BaseObserver<>(new BaseObserverCallBack<ApiResponse<VideoLikeListBean>>() {
            @Override
            public void onSuccess(ApiResponse<VideoLikeListBean> data) {
                sw_layout.setRefreshing(false);
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                if (null != data.getData().getInfo().get(0).getResult()) {
                                    if (null != data.getData().getInfo().get(0).getResult().getRecords()) {
                                        mList.addAll(data.getData().getInfo().get(0).getResult().getRecords());
                                    }
                                }
                            }
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                sw_layout.setRefreshing(false);
            }
        }));
    }


    /**
     * 初始化列表
     */
    private void initRecyclerView() {

        rv_data.setLayoutManager(new GridLayoutManager(getContext(), 2));

        mAdapter = new CommonRecyclerViewAdapter<VideoLikeListBean.InfoDTO.ResultDTO.RecordsDTO>(getContext(), mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, VideoLikeListBean.InfoDTO.ResultDTO.RecordsDTO entity, int position) {
                GlideImgManager.glideLoader(getContext(), entity.getCover(), holder.getView(R.id.iv_long_video_image), 1);
                GlideImgManager.glideLoader(getContext(), entity.getAvatar(), holder.getView(R.id.iv_long_video_head), 0);
                holder.setText(R.id.tv_ranking_nickname, entity.getNickName());
                if (!StringUt.isEmpty(entity.getIsvip())) {
                    if (entity.getIsvip().equals("1")) {
                        ((ImageView) holder.getView(R.id.iv_vip)).setVisibility(View.VISIBLE);
                    } else {
                        ((ImageView) holder.getView(R.id.iv_vip)).setVisibility(View.GONE);
                    }
                }
                if (!StringUt.isEmpty(entity.getAuthentication())) {
                    if (entity.getAuthentication().equals("1")) {
                        ((ImageView) holder.getView(R.id.iv_goddess)).setVisibility(View.VISIBLE);
                    } else {
                        ((ImageView) holder.getView(R.id.iv_goddess)).setVisibility(View.GONE);
                    }
                }
                holder.setText(R.id.tv_time, entity.getCreateTime());

                holder.setText(R.id.text_view1, entity.getComments());
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_comment_long_video;
            }
        };

        rv_data.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                startActivity(new Intent(getContext(),
                        ShortVideoDetailsAc.class)
                        .putExtra("id", mList.get(position).getVid())
                        .putExtra("video", mList.get(position).getVideo())
                );
            }
        });
    }


    //改变bitmap尺寸的方法
    public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }

}
